package ipmsg.etc;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

public class FileLinkList {
	private String ip;
	private int packetNo;
	private boolean active = false;
	private ArrayList<FileNode> files = new ArrayList<FileNode>();

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return this.active;
	}

	public String getIp() {
		return this.ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setPacketNo(int packetNo) {
		this.packetNo = packetNo;
	}

	public int getPacketNo() {
		return this.packetNo;
	}

	public ArrayList<FileNode> getFiles() {
		return this.files;
	}

	public boolean isTanstered() {
		boolean res = true;
		Iterator<FileNode> it = this.files.iterator();
		while (it.hasNext()) {
			FileNode obj = (FileNode) it.next();
			if (!obj.isIsTransfered()) {
				res = false;
				break;
			}
		}
		return res;
	}

	public Vector<String> exportFileList() {
		int count = 0;
		Vector<String> tmp = new Vector<String>();
		Iterator<FileNode> it = this.files.iterator();
		while (it.hasNext()) {
			FileNode obj = (FileNode) it.next();
			if (!obj.isIsTransfered())
				tmp.add(count + " . " + obj.getFileName());
			count++;
		}
		return tmp;
	}

	public String exportPacket() {
		StringBuffer sb = new StringBuffer();
		Iterator<FileNode> it = getFiles().iterator();
		while (it.hasNext()) {
			FileNode obj = (FileNode) it.next();
			sb.append(Integer.toHexString(obj.getFileNo()) + ":" + new File(obj.getFileName()).getName() + ":"
					+ Long.toHexString(obj.getFileLen()) + ":" + 0 + ":" + Integer.toHexString(obj.getFileKind()) + ":"
					+ '\007');
		}

		return sb.toString();
	}

	public void show() {
		System.out.println("ip : " + this.ip);
		for (int i = 0; i < this.files.size(); i++) {
			if (this.files.get(i) != null) {
				System.out.println("序列号 ： " + ((FileNode) this.files.get(i)).getFileNo());
				System.out.println("文件名 : " + ((FileNode) this.files.get(i)).getFileName());
				System.out.println("长度 : " + ((FileNode) this.files.get(i)).getFileLen());
				System.out.println("类型 : " + ((FileNode) this.files.get(i)).getFileKind());
			} // end if
		} // end for
	}

	public static FileLinkList createFileLinkList(String msg, String ip, int packetNo) {
		FileLinkList res = new FileLinkList();

		res.ip = ip;
		res.packetNo = packetNo;

		int pos = 0;
		int prePos = 0;

		for (;;) {
			pos = 0;
			prePos = pos;
			FileNode node = new FileNode();
			for (int i = 0; i < 5;) {
				int count = 1;
				if ((pos = msg.indexOf(":", pos)) < 0) {
					return null;
				}
				while (msg.charAt(++pos) == ':') {
					count++;
				}
				if (count % 2 != 0) {
					String tmpStr = msg.substring(prePos, pos - 1);
					switch (i) {
					case 0:
						if (!node.setFileNo(tmpStr))
							return null;
						break;
					case 1:
						node.setFileName(tmpStr);
						break;
					case 2:
						if (!node.setFileLen(tmpStr)) {
							return null;
						}
						break;
					case 3:
						node.setTime(tmpStr);
						break;
					case 4:
						if (!node.setFileKind(tmpStr)) {
							return null;
						}
						break;
					default:
						return null;
					}
					prePos = pos;
					i++;
				}
			}
			res.files.add(node);
			pos = msg.indexOf('\007', pos);
			msg = msg.substring(pos + 1);
			if (msg.indexOf('\007') < 0)
				break;
		} // end for
		return res;
	}

}
